import { constantRouterMap, systems, configures, patchs, users, logs } from '@/router'
import { getPermission } from "@/api/role"
import { hasPermission } from "@/utils/auth";

function getPath(routes) {
    for (let i = 0; i < routes.length; i++) {
        let route = routes[i];
        if (!route.hidden) {
            if (route.meta && route.meta.header_title) {
                return route.path
            } else {
                let path = route.children.length > 0 ? getPath(route.children) : undefined;
                if (path != undefined) {
                    return path.startsWith(route.path) ? path : (route.path + "/" + path)
                }
            }
        }
    }
}

function filterAsyncRouter(routers, menus) {
    routers.forEach((route) => {
        if (!hasPermission(menus, route)) {
            route.hidden = true;
        }
        route.children && filterAsyncRouter(route.children, menus)
    })
    return routers
}

const permission = {
    state: {
        routers: constantRouterMap,
        hostRouters: [],
        configureRouters: [],
        patchRouters: [],
        logRouters: [],
        userRouters: [],
        notfound: [],
        dashboard: '',
        menus: [],
        operations: [],
        activePanel: ''
    },
    mutations: {
        SET_HOSTROUTERS: (state, routers) => {
            state.hostRouters = routers;
        },
        SET_MENUS: (state, menus) => {
            state.menus = menus
        },
        SET_OPERATIONS: (state, operations) => {
            state.operations = operations;
        },
        SET_CONFIGUREROUTERS: (state, routers) => {
            state.configureRouters = routers;
        },
        SET_PATCHROUTERS: (state, routers) => {
            state.patchRouters = routers;
        },
        SET_LOGROUTERS: (state, routers) => {
            state.logRouters = routers;
        },
        SET_USERROUTERS: (state, routers) => {
            state.userRouters = routers;
        },
        SET_ACTIVEDASHBOARD: (state, dashboard) => {
            state.dashboard = dashboard;
        },
        SET_ACtiVEPANEL: (state, panel) => {
            state.activePanel = panel;
        },
        SET_NOTFOUND: (state, routers) => {
            state.notfound = routers;
        }
    },
    actions: {
        GenerateRoutes({ commit, state }) {
            return new Promise(resolve => {
                const menus = state.menus;
                let hostRouters, configureRouters, patchRouters, logRouters, userRouters
                hostRouters = filterAsyncRouter(JSON.parse(JSON.stringify(systems)), menus)
                configureRouters = filterAsyncRouter(JSON.parse(JSON.stringify(configures)), menus)
                patchRouters = filterAsyncRouter(JSON.parse(JSON.stringify(patchs)), menus)
                logRouters = filterAsyncRouter(JSON.parse(JSON.stringify(logs)), menus)
                userRouters = filterAsyncRouter(JSON.parse(JSON.stringify(users)), menus)
                commit('SET_HOSTROUTERS', hostRouters)
                commit('SET_CONFIGUREROUTERS', configureRouters)
                commit('SET_PATCHROUTERS', patchRouters)
                commit('SET_LOGROUTERS', logRouters)
                commit('SET_USERROUTERS', userRouters)
                resolve()
            })
        },
        getPermission({ commit }, ids) {
            return getPermission({ ids }).then(res => {
                return new Promise((resolve, reject) => {
                    if (res.data.code === "0") {
                        let data = res.data.data;
                        let { menu, operation } = data;
                        let arr = []
                        operation.forEach(item => {
                            arr.push(item.name)
                        })
                        commit("SET_MENUS", menu);
                        commit("SET_OPERATIONS", arr);
                        resolve()
                    } else {
                        reject()
                    }
                })
            })
        },
        SetDashboard({ commit }, dashboard) {
            commit("SET_ACTIVEDASHBOARD", dashboard)
        },
        SetMenus({ commit }, menus) {
            commit("SET_MENUS", menus)
        },
        SetActivePanel({ commit }, panel) {
            commit("SET_ACtiVEPANEL", panel)
        }
    },
    getters: {
        addRoutes: state => {
            const { hostRouters, configureRouters, patchRouters, logRouters, userRouters, notfound } = state;
            return [...hostRouters, ...configureRouters, ...patchRouters, ...logRouters, ...userRouters, ...notfound];
        },
        getMenus: state => {
            return state.menus
        },
        getOperations: state => {
            return state.operations
        },
        getDashboards: state => {
            let dashboards = []
            const { hostRouters, configureRouters, patchRouters, logRouters, userRouters } = state;
            if (getPath(hostRouters)) {
                dashboards.push("system.dashboard")
            }
            if (getPath(configureRouters)) {
                dashboards.push("configure.dashboard")
            }
            if (getPath(patchRouters)) {
                dashboards.push("patch.dashboard")
            }
            if (getPath(userRouters)) {
                dashboards.push("user.dashboard")
            }
            if (getPath(logRouters)) {
                dashboards.push("log.dashboard")
            }
            return dashboards
        },
        getPermissionRouters: state => {
            let routers = [];
            switch (state.dashboard) {
                case "system.dashboard":
                    routers = state.hostRouters;
                    break;
                case "configure.dashboard":
                    routers = state.configureRouters
                    break;
                case "patch.dashboard":
                    routers = state.patchRouters
                    break;
                case "user.dashboard":
                    routers = state.userRouters
                    break
                case "log.dashboard":
                    routers = state.logRouters
                    break
                default:
                    routers = []
            }
            return routers;
        },
        getPaths: state => {
            let data = { };
            const { hostRouters, configureRouters, patchRouters, logRouters, userRouters } = state;
            if (getPath(hostRouters)) {
                data['system.dashboard'] = getPath(hostRouters);
            }
            if (getPath(configureRouters)) {
                data['configure.dashboard'] = getPath(configureRouters)
            }
            if (getPath(patchRouters)) {
                data['patch.dashboard'] = getPath(patchRouters);
            }
            if (getPath(userRouters)) {
                data["user.dashboard"] = getPath(userRouters);
            }
            if (getPath(logRouters)) {
                data["log.dashboard"] = getPath(logRouters);
            }
            return data;
        }
    }
}

export default permission